#include "Host.h"
#include "NodeServer.h"
#include "Game.h"
#include "cache.h"
#include "DbPart.h"

CHostBase* CreateHost()
{
	CHost* pHost = new CHost();
	return pHost;
}

std::string GetLogFileName()
{
	return "agency";
}

CHost::CHost()
{
	m_bCacheConnOK = false;
}

CHost::~CHost()
{
}


void CHost::Init()
{
	this->m_pNode = GetNodeServer();
	this->m_pGame = new CGameServer();
	this->m_pGame->m_pHost = this;
	m_bGameInit = false;
	SpiderEvent ev;
	ev.evID = NODE_EV_SET_TYPE;
	ev.evParam.iParam = SERVER_TYPE_AGENCY;
	m_pNode->ProcEvent(NODE_EV_SET_TYPE, &ev);
	
	vector<CONFITEM> items;
	CONFITEM conf_item;
	conf_item.type = MASTER_CONF_DB;
	conf_item.name = "db_agency";
	items.push_back(conf_item);

	ev.evID = NODE_EV_SET_CACHE;
	ev.evParam.ptr = &items;
	m_pNode->ProcEvent(NODE_EV_SET_CACHE, &ev);


}


int CHost::OnBYTimeOut(int Timerid)
{
	return 0;
}

int CHost::ProcEvent(int ev_id, SpiderEvent* pEvent)
{
	if (HOST_EV_CACHE_CONN_SUCCESS == ev_id)
	{
		CONFITEM* item = (CONFITEM*)pEvent->evParam.ptr;
		if (MASTER_CONF_DB == item->type)
		{
			if ("db_agency" == item->name)
			{
				CBaseDbPart* pHandler = m_pNode->GetDBHandler(item->name);
				CDbPart::Instance()->m_dbHandler = pHandler;
			}
		}
		this->m_bCacheConnOK = true;
	}
	if (HOST_EV_GAME_INIT == ev_id && !m_bGameInit)
	{
		m_bGameInit = true;
		this->m_pGame->InitGame();
	}
	return 0;
}


int CHost::ProcPacket(NETInputPacket* inputPkg, SocketHandler* pSocketHandler)
{
	if (NULL != m_pGame )
	{
		return m_pGame->ProcessPacket(pSocketHandler, inputPkg);
	}
	return 0;
}